<?php

class DefaultController extends ControllerModuleBackup
{
	public function actionIndex()
	{
		$backupForm = new BackupForm();
		$this->render('index', array(
			'backupForm' => $backupForm
		));
	}
	
	public function actionDo()
	{
		$now = time();
		if(app()->request->isAjaxRequest){
			$id_cpanel_account = app()->request->getParam('id_cpanel_account', null);
			$cpanelAccount = $this->loadModelByPk($id_cpanel_account, 'CpanelAccounts');
			$whmaccount = $this->loadModelByPk($cpanelAccount->id_account, 'WhmAccount');
			$whmApi = new WhmApi($whmaccount);
			$ret = false;
			$backupResult = $errors = null;
			if($ret = $whmApi->AccountAuthByHash()){
				$backupDetails = app()->request->getPost('BackupForm', null);
				$backupForm = new BackupForm();
				$backupForm->attributes = $backupDetails;
				if($backupForm->validate()){
					$backupResult = $whmApi->Cpanel_Backup_API1($cpanelAccount->username, $backupDetails);		
				}else{
					$ret = false;
					$whmApi->addErrors($backupForm->getErrors());
				}
			}
			if(!$ret){
				$errors = $whmApi->getErrors();
			}

			$this->_returnAjax(array(
				'result' => $ret,
				'errors' => $errors,
				'backupResult' => $backupResult,
				'time' => $now
			));
		}
	}
	
	public function accessRules()
	{
		return array(
			array(
				'allow',
				'actions' => array('index', 'do'),
				'expression' => 'app()->user->isLogin() && app()->user->hasManageBackupPerm()',
				'redirect' => array('default/noaccess')
			),

			array(
				'deny',	//should be 'deny', 'allow' is used to give convenient to make test
				'users' => array('*'),
				'redirect' => array('user/login')
			)
		);
	}
}